package org.mindrive.system.web.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.config.JwtTokenUtil;
import org.mindrive.system.core.MindriveException;
import org.mindrive.system.core.Result;
import org.mindrive.system.parameter.system.LoginParameter;
import org.mindrive.system.parameter.system.UserParameter;
import org.mindrive.system.service.system.UserService;
import org.mindrive.system.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = { "认证管理" })
@RestController
@Slf4j
public class AuthenticationController extends BaseController {

	@Autowired
	private AuthenticationManager authenticationManager;

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	@Autowired
	private UserDetailsService userDetailsService;

	@Autowired
	private UserService userService;

	@ApiOperation(value = "登录", notes = "登录")
	@PostMapping(value = "/login")
	public Result login(@Validated @RequestBody LoginParameter parameter, BindingResult bindingResult) {

		Result result = new Result();

		validParams(bindingResult);

//		userService.getUserByUserName(parameter.getUsername());

		try {
			//用户认证
			authenticationManager.authenticate(
					new UsernamePasswordAuthenticationToken(parameter.getUsername(), parameter.getPassword()));
		}catch (BadCredentialsException e){
			throw new MindriveException("账号或密码错误,请重新输入");
		}catch (UsernameNotFoundException e){
			throw new MindriveException("账号错误");
		}

		final UserDetails userDetails = userDetailsService.loadUserByUsername(parameter.getUsername());

		final String token = "Bearer " + jwtTokenUtil.generateToken(userDetails);

		result = Result.ok(token);
		return result;
	}

	@ApiOperation(value = "注册", notes = "注册")
	@PostMapping(path = "/register")
	public Result register(@RequestBody UserParameter parameter) {
		log.info("注册开始。");
		Result result = new Result();
		try {
			UserEntity data = userService.register(parameter);
			result = Result.ok(data);
		} catch (Exception ex) {
			log.error("注册失败。", ex);
			result = Result.error(ex.getMessage());
		}
		log.info("查询用户列表结束。");
		return result;
	}

}
